import { JSEncrypt } from './jsencrypt';
import request from '../request';
import util from '../util/index.js';

function encrypt(publicKey,encryptStr) {
    let jsEncrypt = new JSEncrypt();
    jsEncrypt.setPublicKey(publicKey);
    return  jsEncrypt.encrypt(encryptStr);
}

function kantbootEncrypt(publicKey,encryptStr) {
    return publicKey+"&&"+encrypt(publicKey,encryptStr);
}

/**
 * 向服务器索取公钥，并加密数据
 * 后端得使用kantboot框架
 */
async function kantbootEncryptByServer(encryptStr) {
    let url = "/functional-common-web/kantbootRsa/generatePublicKey";
    return new Promise((resolve, reject) => {
        request.request({
            url: url,
            method: "POST",
            stateSuccess: (res) => {
                let publicKey = res.data;
                let result=kantbootEncrypt(publicKey,encryptStr);
                resolve(result);
            }
        });
    });
}


export default {
    encrypt,
    kantbootEncrypt,
    kantbootEncryptByServer
}